package com.own.business.wallet.controller.user;

import com.own.business.wallet.business.service.WalletService;
import com.own.business.wallet.controller.user.form.ChangePayPasswordForm;
import com.own.business.wallet.controller.user.form.GetChangeSensitiveTokenForm;
import com.own.business.wallet.entity.map.WalletMapVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.component.log.operation.aop.LogOperationModuleAop;
import com.own.component.sensitive.check.SensitiveTokenUtil;
import com.own.component.sensitive.check.entity.SensitiveTokenVo;
import com.own.component.store.core.util.base.RsaKeyUtil;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * UserWalletController
 *
 * @author chenxueli
 * @date 2023-07-13 16:07:39
 */
@Tag(name = "钱包" + ConstantBeanSuffix.USER_CLIENT + ConstantBeanSuffix.CONTROLLER)
@LogOperationModuleAop("钱包")
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "business/wallet")
public class UserWalletController extends BaseController {

    @Resource
    private WalletService walletService;
    @Resource
    private SensitiveTokenUtil sensitiveTokenUtil;

    @Resource
    private RsaKeyUtil rsaKeyUtil;

    @LogOperationAop
    @Operation(summary = "查询个人的钱包信息")
    @GetMapping("/self")
    public ResultModel<WalletMapVo> self() {
        var loginUser = SessionUserUtil.getLoginUser();
        var po = walletService.getByUserId(loginUser.userId());
        return R.success(new WalletMapVo(po));
    }

    @LogOperationAop
    @Operation(summary = "获取有没有设置密码")
    @GetMapping("/check/set/password")
    public ResultModel<Boolean> isSetPassword() {
        var loginUser = SessionUserUtil.getLoginUser();
        var wallet = walletService.getByUserId(loginUser.userId());
        var flag = StringUtils.isNotBlank(wallet.getPassword());
        return R.success(flag);
    }

    @LogOperationAop
    @Operation(summary = "修改支付密码")
    @PostMapping("/change/password")
    public ResultModel<Boolean> changePayPassword(
            @RequestBody ChangePayPasswordForm form
    ) {
        form.check();
        var loginUser = SessionUserUtil.getLoginUser();
        // 验证铭感信息token
        sensitiveTokenUtil.checkToken(loginUser.userId(), form.getToken(), form.getKey());
        // 获取真实的密码
        var realPassword = rsaKeyUtil.decrypt(form.getKey(), form.getPassword());
        // 进行修改密码
        walletService.updatePassword(loginUser.userId(), realPassword);
        return R.success(true);
    }

    @LogOperationAop
    @Operation(summary = "根据支付密码获取敏感信息token")
    @PostMapping("/get/change/token")
    public ResultModel<SensitiveTokenVo> getSensitiveChangeToken(
            @RequestBody GetChangeSensitiveTokenForm form
    ) {
        var loginUser = SessionUserUtil.getLoginUser();
        var vo = sensitiveTokenUtil.getToken(loginUser.userId(), form.getPublicKey(), () -> {
            // 获取真实的密码
            var password = rsaKeyUtil.decrypt(form.getKey(), form.getPassword());
            // 验证密码是否正确
            var flag = walletService.checkPassword(loginUser.userId(), password);
            if (!flag) {
                throw new BusinessSimpleException("原密码错误");
            }
        });
        return R.success(vo);
    }

}
